﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.ComponentModel;
using System.ComponentModel.Design;
//using System.Windows.Forms;
//using System.Windows.Forms.Design;

using DotSpatial.Data;

using DotSpatial.Topology;

using DotSpatial.Symbology;

using FXnaInfrastructure;



namespace FXnaGis
{
    [Serializable]
    /// <summary>
    /// 公共基础图层，基础标注系统，选择系统，碰撞检测
    /// </summary>
    public abstract class FLayer:FEntity, IFLayer
    {


        public FLayer(string id):base(id)
        {

          

 
        }





        #region 事件



        /// <summary>
        /// 要求重绘事件
        /// </summary>

      


        #endregion




        #region 字段属性



        [NonSerialized]
        FMapFrame _mapFrame;

        [Browsable(false)]
        public virtual FMapFrame MapFrame
        {
            get { return _mapFrame; }
            set { _mapFrame = value; }
        }





        FGroupLayer _parent;

        [Browsable(false)]
        /// <summary>
        /// 在最底层提供多层次的能力
        /// </summary>
        public FGroupLayer Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }




        IDGenerator _idGenerator=new IDGenerator ();

        [Browsable(false)]
        public IDGenerator IdGenerator
        {
            get { return _idGenerator; }

        }





        protected bool _isShowLable = true;

        [Category("可见性"), DisplayName("标注")]
        /// <summary>
        /// 是否显示标注
        /// </summary>
        public bool IsShowLable
        {
            get { return _isShowLable; }
            set { _isShowLable = value; }
        }



        LableStyle _lableStyle=new LableStyle ();

        [Category("样式设置"),DisplayName("标注"),TypeConverter(typeof (ExpandableObjectConverter))]
        /// <summary>
        /// 本体标注样式
        /// </summary>
        public LableStyle LableStyle
        {
            get { return _lableStyle; }
            set { _lableStyle = value; }
        }



        protected bool _isVisiable = true;

        /// <summary>
        /// 图层是否可见
        /// </summary>
        [Category("图层设置"), DisplayName("图层是否可见"), Browsable(false)]
        public bool IsVisible
        {
            get { return this._isVisiable; }
            set
            {
                if (this._isVisiable!=value)
                {

                    this._isVisiable = value;
                    this.OnSetVisible();
                    
                }
              
            }
        }


        private float _Opacity = 1.0F;

        /// <summary>
        /// 图层透明度
        /// </summary>
        [Category("可见性"), DisplayName("透明度")]
        public virtual float Opacity
        {
            get { return _Opacity; }
            set
            {
                if (value > 1.0F)
                {
                    value = 1.0F;
                }
                else if (value < 0.0F)
                {
                    value = 0.0F;
                }
                _Opacity = value;
                this.OnSetLayerOpacity();
            }
        }

        protected virtual void OnSetLayerOpacity()
        {

            this.AskRefresh();

        }



        protected virtual void OnSetVisible()
        {
         
            //可见性重绘

            this.AskRefresh();


        }





        /// <summary>
        /// 层内图形是否可以选择
        /// </summary>
        protected bool _isSelectable = false;

        [DisplayName("图层是否可选择"), Browsable(false)]
        public bool IsSelectable
        {
            get { return _isSelectable; }
            set
            {
                _isSelectable = value;
                this.OnSetSelectable();
            }
        }

        protected virtual void OnSetSelectable()
        {
            

        }



        /// <summary>
        /// 所有图形的地理边界
        /// </summary>
       public virtual Extent Extent
        {
            get
            {
                return null;
            }
        }


        [Browsable(false)]
        /// <summary>
        /// 所有已经选择了的物体边界
        /// </summary>
       public virtual Extent SelectedExtent
       {
           get 
           {
               return null;
           }
       }



        #endregion


        /// <summary>
        /// This will draw any features that intersect this region.  To specify the features
        /// directly, use OnDrawFeatures.  This will not clear existing buffer content.
        /// For that call Initialize instead.
        /// </summary>
        /// <param name="args">A GeoArgs clarifying the transformation from geographic to image space</param>
        /// <param name="regions">The geographic regions to draw</param>
        public virtual  void DrawRegions(FMapArgs args, List<Extent> regions)
        {

            if (!this._isVisiable)
            {
                return;
                
            }

            this.DoCollsionDectection(args,regions);

            this.DrawCore(args,regions);

            if (this._isShowLable)
            {
                this.DrawLable(args,regions);
                
            }




        }


        public bool VisibleAtExtent(Extent ViewExtents)
        {
            Extent ext=this.Extent;

            if (ext!=null)
            {
                return ext.Intersects(ViewExtents);
                
            }

            return true;
        }





        /// <summary>
        /// 要求重绘自身
        /// </summary>
        public virtual void AskRefresh()
        {

            if (this._mapFrame!=null)
            {
                this._mapFrame.RedrawBuffer();
                
            }
 

             

        }


        #region 碰撞封装函数

        /// <summary>
        /// 实施碰撞检测
        /// </summary>
        protected virtual void DoCollsionDectection(FMapArgs args, List<DotSpatial.Data.Extent> regions)
        {
 
        }

        /// <summary>
        /// 实际绘制的函数，这里不在考虑多余的问题,比如可见性，冻结性，是否显示等
        /// </summary>
        /// <param name="args"></param>
        /// <param name="regions"></param>
        protected virtual void DrawCore(FMapArgs args, List<Extent> regions)
        {

 
        }


        protected virtual void DrawLable(FMapArgs args, List<Extent> regions)
        {
 
        }


        #endregion


        #region 选择系统


        public virtual void Select(IEnvelope tolerant, IEnvelope strict, FSelectionMode selectionMode,
                                    out IEnvelope affectedArea)
        {


            affectedArea = null;


            //如果不可选择，则直接返回

            if (!this._isSelectable)
            {
                return;


                
            }




        }

        /// <summary>
        /// 常规的鼠标选择函数
        /// </summary>
        /// <param name="selectArea"></param>
        /// <param name="smode"></param>
        /// <param name="omode"></param>
        public virtual void Select(Extent selectedArea, FSelectionMode smode, FSelectionOperationMode omode)
        {


            //如果不可选择，则直接返回

            if (!this._isSelectable)
            {
                return;



            }




        }



        public virtual void Select(string id, FSelectionMode smode, FSelectionOperationMode omode)
        {

            //如果不可选择，则直接返回

            if (!this._isSelectable)
            {
                return;



            }


           





        }


        /// <summary>
        /// 去掉特定的选择
        /// </summary>
        /// <param name="id"></param>
        public virtual void UnSelect(string id)
        {
           

        }



        /// <summary>
        /// 全选操作
        /// </summary>
        public virtual void SelectAll()
        {

            //如果不可选择，则直接返回

            if (!this._isSelectable)
            {
                return;



            }





        }


        /// <summary>
        /// 清空选择
        /// </summary>
        public virtual void UnSelectAll()
        {
           

        }


       



        #endregion

        #region 标注系统



        #endregion



    }
}
